$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        taskStatsList:null,
        depId:0,
        sStaffName:null,
        startTime:null,
        endTime:null,
        init:function (){
            $('#startTime').datebox({showSeconds: true});
            $('#endTime').datebox({showSeconds: true});
            this.funObj().initDepTree();
            this.taskStatsList = new xhDataGrid($('#statsList'),'pm/taskstats/getTaskStats',{sStaffName:this.sStaffName,iDepId:this.depId,startTime:this.startTime,endTime:this.endTime});
            this.taskStatsList.parseToolbar().init();
            this.listener();
        },
        listener:function () {
            var $this = this;
            $('#bt_list_search').click(function () {
                $this.sStaffName = $('#staffName').val();
                $this.startTime = $('#startTime').datebox('getValue');
                $this.endTime = $('#endTime').datebox('getValue');
                $this.taskStatsList.reload({sStaffName:$this.sStaffName,iDepId:$this.depId,startTime:$this.startTime,endTime:$this.endTime});
            });
        },
        funObj:function () {
            var $this = this;
            return {
                initDepTree:function () {
                    $('#depList').treegrid({
                        url: moduleUrl+'taskstats/getDepartments',
                        method: 'post',
                        idField: 'iDepId',
                        treeField: 'sDepName',
                        singleSelect:true,
                        columns:[[
                            {field:'ck',checkbox:true},
                            {field:'iDepId',title:'iDepId',hidden:true},
                            {field:'sDepName',title:'名称',width:300,align:'left'},
                        ]],
                        onSelect:function (row) {
                            $this.depId = row['iDepId'];
                            $('#depList').treegrid('expand',$this.depId);
                            $this.taskStatsList.reload({sStaffName:$this.sStaffName,iDepId:$this.depId,startTime:$this.startTime,endTime:$this.endTime});
                        },
                        onUnselect:function () {
                            $this.taskStatsList.reload({sStaffName:0,iDepId:0,startTime:0,endTime:0});
                        },
                        onBeforeSelect:function (row) {
                            if($this.depId == 0){
                                return true;
                            }else {
                                if($this.depId == row['iDepId']){
                                    $('#depList').treegrid('unselect',$this.depId);
                                    $('#depList').treegrid('collapse',$this.depId);
                                    $this.depId = 0;
                                    // $this.taskStatsList.clear();
                                    // $this.title = '任务统计列表';
                                    // $('.xh-datagrid .panel-title').html(title);
                                    // $this.taskStatsList.reload({selectProjectId:projectid});
                                    return false;
                                }
                            }
                        },
                        onBeforeCheck:function (row) {
                            if($this.depId == 0){
                                return true;
                            }else {
                                if($this.depId == row['iDepId']){
                                    $('#depList').treegrid('unselect',$this.depId);
                                    $('#depList').treegrid('collapse',$this.depId);
                                    $this.depId = 0;
                                    // $this.taskStatsList.clear();
                                    // $this.title = '任务统计列表';
                                    // $('.xh-datagrid .panel-title').html(title);
                                    // $this.taskStatsList.reload({selectProjectId:projectid});
                                    return false;
                                }
                            }
                        },
                    });
                },


            }
        }
    }
};
